package czopyk.model.persistence.actions;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import czopyk.model.persistence.ValueExtractor;

import pl.czopyk.view.QuickDrawer;

public class VisualizeXYAction extends CommonAction implements Action {

	private Logger log = Logger.getLogger(getClass());
	
	String label, chart, varX, varY;
	boolean pdf;

	public VisualizeXYAction(String varX, String varY, String label,
			String chart, ValueExtractor ve) {
		super(null, ve);
		this.label = label == null ? varY : label;
		this.chart = chart == null ? "" : chart;

		if (varX == null) {
			varX = "time";
		}
		this.varX = varX;

		if (varY == null) {
			throw new IllegalStateException("VisualizeXY must specify varY.");
		}
		this.varY = varY;

	}

	public void perform() {
		String l = ve.getValue(label);
		String c = ve.getValue(chart);

		BigDecimal x = ve.getNumber(varX);
		BigDecimal y = ve.getNumber(varY);

		if (x == null) {
			log.warn("Variable '" + varX
					+ "' has no value (visualizeXY).");
			return;
		}
		;

		if (y == null) {
			log.warn("Variable '" + varY
					+ "' has no value (visualizeXY).");
			return;
		}
		;

		QuickDrawer.addXY(x.doubleValue(), y.doubleValue(), l, c);
	}
}

